﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using MM.DBUtility;
using MM.Common;
using System.Collections;

namespace WebMMS.Base
{
    public partial class APPID : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Session["CurUser"] == null)
                {
                    Response.Redirect("~/Login.aspx");
                    return;
                }
                if (!IsPostBack)
                {
                    AspNetPager1.CurrentPageIndex = 1;
                    GVDataBind();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        protected void btnADD_Click(object sender, EventArgs e)
        {
            try
            {
                string strNO = txtNO.Text.Trim();
                if (string.IsNullOrEmpty(strNO))
                {
                    Common.PageStartAlert(this.Page, "APPID不能为空！");
                    return;
                }
                if (MM.DAL.RuleInfo.ExistAPPID(strNO))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "APPID已经存在，请更换！");
                    return;
                }
                string strName = txtName.Text.Trim();
                if (string.IsNullOrEmpty(strName))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "描述不能为空！");
                    return;
                }
                string strSQL = string.Format("INSERT INTO T_APPinfo(APPID,APPValue) VALUES('{0}',N'{1}')", strNO, strName);
                MM.DBUtility.DbHelperSql.ExecuteSql(strSQL);
                MM.Common.Common.PageStartAlert(this.Page, "添加成功！");
                txtNO.Text = "";
                txtName.Text = "";
                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #region GV info
        private void GVDataBind()
        {
            string strSQL = "SELECT a.APPID,a.APPValue,a.CreateTime FROM T_APPinfo a WITH(NOLOCK) WHERE 1=1";
            if (!string.IsNullOrEmpty(txtNO.Text.Trim()))
                strSQL += string.Format(" AND a.APPID = '{0}'", txtNO.Text.Trim());
            if (!string.IsNullOrEmpty(txtName.Text.Trim()))
                strSQL += string.Format(" AND a.APPValue LIKE N'%{0}%'", txtName.Text.Trim());

            string strOrder = " ORDER BY a.CreateTime DESC";

            AspNetPager1.RecordCount = DbHelperSql.GetSingle("SELECT COUNT(1) FROM (" + strSQL + ") AS B").ToIntOr();

            if (AspNetPager1.CurrentPageIndex == 1)
                strSQL = string.Format(strSQL.Replace("SELECT ", "SELECT TOP {0} "), AspNetPager1.PageSize) + strOrder;
            else
                strSQL = string.Format("SELECT TOP {0} * FROM ({1}) AS F WHERE rn BETWEEN {2} AND {3}", AspNetPager1.PageSize, strSQL.Replace("SELECT ", string.Format("SELECT row_number() over({0}) rn, ", strOrder)), (AspNetPager1.CurrentPageIndex - 1) * AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex * AspNetPager1.PageSize - 1);

            DataSet ds = DbHelperSql.Query(strSQL);
            GVinfo.DataSource = ds;
            GVinfo.DataBind();
            ViewState["DataTemp"] = ds;
        }
        protected void GVinfo_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            try
            {
                GVinfo.EditIndex = -1;
                GVinfo.DataSource = (DataSet)ViewState["DataTemp"];
                GVinfo.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void GVinfo_RowDataBound(object sender, GridViewRowEventArgs e)
        {
        }

        protected void GVinfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                string strNO = ((HiddenField)GVinfo.Rows[e.RowIndex].FindControl("HF2")).Value;
                ArrayList alist = new ArrayList();
                alist.Add(string.Format("DELETE FROM T_APPinfo WHERE APPID='{0}'", strNO));
                alist.Add(string.Format("DELETE FROM T_Rule WHERE APPID='{0}'", strNO));
                alist.Add(string.Format("DELETE FROM T_Timers WHERE APPID='{0}'", strNO));
                DbHelperSql.ExecuteSqlTran(alist);
                MM.Common.Common.PageStartAlert(this.Page, "删除成功！");
                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void GVinfo_RowEditing(object sender, GridViewEditEventArgs e)
        {
            try
            {
                GVinfo.EditIndex = e.NewEditIndex;
                GVinfo.DataSource = (DataSet)ViewState["DataTemp"];
                GVinfo.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void GVinfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                string strNO = ((HiddenField)GVinfo.Rows[e.RowIndex].FindControl("HF1")).Value;

                string strName = ((TextBox)GVinfo.Rows[e.RowIndex].FindControl("txtName")).Text.Trim();
                if (string.IsNullOrEmpty(strName))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "描述不能为空！");
                    return;
                }

                MM.DBUtility.DbHelperSql.ExecuteSql(string.Format("UPDATE T_APPinfo SET APPValue=N'{0}' WHERE APPID='{1}'", strName, strNO));
                MM.Common.Common.PageStartAlert(this.Page, "更新成功！");
                GVinfo.EditIndex = -1;
                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            GVDataBind();
        }
        #endregion
    }
}